import os
import traceback

import pymysql

from air_web.data_platform import mysql_con


class InitTable:
    def __init__(self, *args, **kwargs):
        pass

    def main(self):
        conn, cursor = mysql_con.get_cursor()

        script_path = os.path.dirname(os.path.abspath(__file__))
        sql_file = os.path.join(
            script_path, "init_mysql_file/init_mysql_sichuan.sql"
        )
        # 打开SQL文件并逐行执行
        with open(sql_file, "r") as file:
            sql_statements = file.read()[:-1].split(";")
            for statement in sql_statements:
                try:
                    cursor.execute(statement)
                except pymysql.err.OperationalError:
                    pass
                except:
                    print(traceback.format_exc())

        # 提交事务并关闭连接
        conn.commit()
        conn.close()
